﻿// **************************************************
// 以下代码由代码生成器生成。
// 请不要修改该文件，以避免您的修改被代码生成器覆盖。
// 如需增加配置，可在DbContext的OnModelCreating方法中添加。
// **************************************************
using Microsoft.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Joy.Erp.BasicArchives.EntityFrameworkCore;

namespace {{namespace}}
{
    public class {{class_name}}ModelConfiguration : IModelConfiguration
    {
        public void Build(ModelBuilder builder)
        {
            builder.Entity<{{class_name}}>(b =>
            {
                b.ToTable({{project_group_name}}DbProperties.DbTablePrefix + nameof({{class_name}}), {{project_group_name}}DbProperties.DbSchema);
                b.ConfigureByConvention();
                b.ConfigureMultiArchiveAggregateRoot<{{class_name}}, {{child_type.class_name}}>();

                {{~ for model_define in model_defines ~}}
                b.Property(x => x.{{model_define.name}}).{{model_define.func}}({{class_name}}Consts.{{model_define.value_property}});
                {{~ end ~}}

                {{~ for navigation_property in navigation_propertys ~}}
                b.HasOne(x => x.{{navigation_property.name}}).WithMany().HasForeignKey(x => x.{{navigation_property.name}}Id).OnDelete(DeleteBehavior.Restrict);
                {{~ end ~}}

                b.HasMany(x => x.Children).WithOne(x => x.MainArchive).HasForeignKey(x => x.MainArchiveId).OnDelete(DeleteBehavior.Cascade);
            });
        }

        public void Entity(IAbpDbContextRegistrationOptionsBuilder options)
        {
            options.Entity<{{class_name}}>(opt =>
            {
                opt.DefaultWithDetailsFunc = query =>
                {
                    return query
                        {{~ for navigation_property in navigation_propertys ~}}
                        .Include(x => x.{{navigation_property.name}})
                        {{~ end ~}}
                        ;
                };
            });
        }
    }
}
